var chartList = []
function chartResize() {
    $(chartList).each(function (i, chart) {
        chart.resize();
    })
}

$(function () {
    loadData();

    bindFootButton();
    bindybType();
    bindrq();
});

function loadData(){

    param.MedType=param.ybType;
    param.NoDept="0";
    param.BeginDate=param.year+"-"+param.month+"-1";
    if(param.month==12){
        param.EndDate=(param.year*1+1)+"-1-1";
    }else{
        param.EndDate=param.year+"-"+(param.month*1+1)+"-1";
    }
    var nextMonth=param.month+1;
    var nextMonthFirstDay=new Date(param.year,nextMonth,1);
    var oneDay=1000*60*60*24;
    param.EndDate= new Date(nextMonthFirstDay-oneDay);
    loadServerData("/analysis/total/queryAll",param,function(data){
        loadChart1(data.datas);

        $("#infoBox1").html('<div class="key">全院'+data.datas[0].typeName+'</div>' +
            '        <div class="value">'+data.datas[0].now+'</div>');

        $("#infoBox2").html('<div class="key">全院'+data.datas[2].typeName+'</div>' +
            '        <div class="value">'+data.datas[2].now+"="+data.datas[2].nowP+'</div>' +
            '        <div class="key">全院'+data.datas[4].typeName+'</div>' +
            '        <div class="value">'+data.datas[4].now+"="+data.datas[4].nowP+'</div>');

        $("#infoBox3").html('<div class="key">全院'+data.datas[1].typeName+'</div>' +
            '        <div class="value">'+data.datas[1].now+"="+data.datas[1].nowP+'</div>' +
            '        <div class="key">全院'+data.datas[3].typeName+'</div>' +
            '        <div class="value">'+data.datas[3].now+"="+data.datas[3].nowP+'</div>');

        $("#infoBox4").html('<div class="key">全院'+data.datas[6].typeName+'</div>' +
            '        <div class="value">'+data.datas[6].now+"="+data.datas[6].nowP+'</div>' +
            '        <div class="key">全院'+data.datas[5].typeName+'</div>' +
            '        <div class="value">'+data.datas[5].now+"="+data.datas[5].nowP+'</div>');

        $("#infoBox5").html('<div class="key">全院'+data.datas[8].typeName+'</div>' +
            '        <div class="value">'+data.datas[8].now+"="+data.datas[8].nowP+'</div>' );

        chartResize();
        loadCartTable();
        bindChartTable();
    },"body");

    var months=[];
    var thisMonth=param.month;
    for(var i=0;i<12;i++){
        if(thisMonth<1){
            thisMonth=12;
        }
        months.unshift(thisMonth+"月")
        thisMonth--;
    }

    loadServerData("/analysis/total/queryAllYear",param,function(data){
        var datas=data.datas;

        loadChart2("chartContainer2",datas, [2, 4],months);
        loadChart2("chartContainer3",datas, [1, 3],months);
        loadChart2("chartContainer4",datas, [6, 5],months);
        loadChart2("chartContainer5", datas,[8],months);

        chartResize();
        loadCartTable();
        bindChartTable();
    });

    param.NoDept=1;
    loadServerData("/analysis/total/queryAllDetailListAllTypes",param,function(data){
        var datas=data.datas;

        loadChart3("chartContainer21",[data.datas[2],data.datas[4]]);
        loadChart3("chartContainer31",[data.datas[1],data.datas[3]]);
        loadChart3("chartContainer41",[data.datas[6],data.datas[5]]);
        loadChart3("chartContainer51",[data.datas[8]]);

        chartResize();
        loadCartTable();
        bindChartTable();
    });
}

function gotoHistory() {
    top.addTabs({
        id: "analysisTotalHistory",
        title: "指标分析",
        close: 'ture',
        url: "/analysis/total/history"
    })

}

function loadChart1(datas) {
    var cont = $("#chartContainer1");
    cont.html("");
    for (var i = 0; i < 9; i++) {
        if(datas[i].type==7) continue;
        var box = $(' <div class="chartBox TA">' +
            '                        <div class="echartBox" id="container" style="width: 100%;height:100%;"></div>' +
            '                        <div class="tableBox" id="table" style="width: 100%;height:100%;">' +
            '                            <div id="title"></div>'+
            '                            <table>' +
            '                                <thead>' +
            '                                <tr>' +
            '                                    <td>项目</td>' +
            '                                    <td>值</td>' +
            '                                </tr>' +
            '                                </thead>' +
            '                                <tbody>' +
            '                                </tbody>' +
            '                            </table>' +
            '                        </div>' +
            '                    </div>');
        cont.append(box);
        if (i == 8) box.addClass("chartBoxLast");
        var option = {
            title: {
                text: datas[i].typeName,
                left: 'center'
            },
            grid : {
                left : 40,    //距离容器上边界40像素
                right: 30   //距离容器下边界30像素
            },
            xAxis: [
                {
                    type: 'category',
                    data: ['标准值', '当前值'],
                    axisTick: {
                        alignWithLabel: true
                    }
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: [
                {
                    type: 'bar',
                    barWidth: '30%',
                    itemStyle: {
                        normal: {
                            color: function (params) {
                                var colorList = [
                                    '#3090a0', '#7030a0'
                                ];
                                return colorList[params.dataIndex]
                            },
                            label: {
                                show: true,
                                position: 'top',
                                formatter: '{b}\n{c}'
                            }
                        }
                    },
                    data: [
                        datas[i].normal, datas[i].nowP
                    ]
                }
            ]
        }
        var dom = box.find("#container")[0];
        var chart = echarts.init(dom);
        chart.setOption(option, true);
        chartList.push(chart);

        var table = box.find("tbody");
        var trs = $("<tr>" +
            "</tr><td>标准值</td><td>" + datas[i].normal + "</td><tr>" +
            "</tr><td>当前值</td><td>" + datas[i].nowP + "</td><tr>" +
            "</tr><td>差异值</td><td>" + datas[i].difference + "</td><tr>" +
            "</tr><td colspan='2'>" + datas[i].now + "</td><tr>" +
            "</tr>");
        box.find("#title").html(datas[i].typeName);
        table.append(trs);
    }
}

function loadChart2(containerId,dataList, dataIndexs,months) {
    var cont = $("#" + containerId);
    cont.html("");
    $(dataIndexs).each(function (index, i) {
        var chartData = [];
        for (var j = 0; j < 12; j++) {
            chartData.push(dataList[j][i].nowP);
        }
        var maxValueTemp = [(dataList[0][i].normal * 1.5).toFixed(5)];
        maxValueTemp = maxValueTemp.concat(chartData)
        var maxValue = Math.max.apply(null, maxValueTemp);
        var box = $(' <div class="chartBox TA">' +
            '                        <div class="echartBox" id="container" style="width: 100%;height:100%;"></div>' +
            '                        <div class="tableBox" id="table" style="width: 100%;height:100%;">' +
            '<div class="scroll">'+
            '                            <div id="title"></div>'+
            '                            <table>' +
            '                                <thead>' +
            '                                <tr>' +
            '                                    <td>月份</td>' +
            '                                    <td>值</td>' +
            '                                    <td>差值</td>' +
            '                                    <td>描述</td>' +
            '                                </tr>' +
            '                                </thead>' +
            '                                <tbody>' +
            '                                </tbody>' +
            '                            </table>' +
            '                       </div> </div>' +
            '                    </div>');
        cont.append(box);
        if (index + 1 == dataIndexs.length) box.addClass("chartBoxLast");
        var option = {
            title: {                                        //标题组件
                text: dataList[0][i].typeName,               //标题文本
                left: 'center'                               //组件离容器左侧的距离,'left', 'center', 'right','20%'
            },
            tooltip: {                                      //提示框组件
                triggerOn: 'mousemove'                          //提示框触发的条件,'mousemove'鼠标移动时触发。'click'鼠标点击时触发。'mousemove|click'同时鼠标移动和点击时触发。'none'不在 'mousemove' 或 'click' 时触发
            },
            toolbox: {                                      //工具框组件
                show: false,                                //是否显示
                left: 'center',                             //组件离容器左侧的距离
                itemSize: 25,                               //工具栏icon的大小
                top: 55,                                   //组件离容器顶部的距离
                feature: {                                 //个性化设置

                    dataZoom: {                            //数据区域缩放
                        yAxisIndex: 'none'                  //对y轴的控制
                    },
                    restore: {}                           //配置项还原
                }
            },

            xAxis: {

                type: 'category',                            //x轴的数据类型：类目轴
                //x轴类目名称
                data: months
            },
            yAxis: {                                    //y轴
                type: 'value',                          //y轴类型：数值轴
                max: maxValue                           //最大值
            },

            dataZoom: [{                                //区域缩放
                type: 'inside',                         //slider表示有滑动块的，inside表示内置的
                throttle: 50                            //设置触发视图刷新的频率。单位为毫秒（ms）
            }],
            series: [//通用
                {
                    name: '当前值',
                    type: 'line',
                    itemStyle: {
                        normal: {
                            color: '#7030a0'
                        }
                    },
                    areaStyle: {                        //区域设置
                        normal: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [     //
                                {
                                offset: 0,
                                color: '#7030a0'
                            }, {
                                offset: 1,
                                color: '#eadef3'
                            }
                            ])
                        }
                    },
                    markLine: {
                        data: [{
                            silent: true,
                            name:"标准值",
                            yAxis: dataList[0][i].normal
                        }]
                    },
                    data: chartData
                }

            ]
        }

        var dom = box.find("#container")[0];
        var chart = echarts.init(dom);
        chart.setOption(option, true);
        chartList.push(chart);

        var table = box.find("tbody");
        for (var j = 1; j < 13; j++) {
            var trs = $("<tr>" +
                "<td>" +months[j-1] +"</td>" +
                "<td>" + dataList[j - 1][i].nowP + "</td>" +
                "<td>" + dataList[j - 1][i].difference + "</td>" +
                "<td>" + dataList[j - 1][i].now + "</td>" +
                "</tr>");
            table.append(trs);
        }
        box.find("#title").html(dataList[0][i].typeName);
    });


}

function loadChart3(containerId,dataList) {
    $(dataList).each(function (index,datas) {
        var chartData = [];
        for (var i = 0; i < datas.length; i++) {
            chartData.push(datas[i].nowP);
        }
        var maxValueTemp = [(datas[0].normal)];
        maxValueTemp = maxValueTemp.concat(chartData)
        var maxValue = Math.max.apply(null, maxValueTemp);
        maxValue=(maxValue*1.5).toFixed(2);
        //排序功能 从大到小
        function sortnowP(a,b){
            return a.nowP-b.nowP
        }
        datas.sort(sortnowP);

        var cont = $("#" + containerId);
        cont.html("");
        var chartDataValue = [];
        var chartDataItem = [];
        for (var i = 0; i < datas.length; i++) {
            chartDataValue.push(datas[i].nowP);
            chartDataItem.push(datas[i].deptName.trim());
        }
        // var maxValueTemp = [(dataList[0][i].normal * 1.5).toFixed(5)];
        // maxValueTemp = maxValueTemp.concat(chartData)
        // var maxValue = Math.max.apply(null, maxValueTemp);
        var box = $(' <div class="chartBox TA">' +
            '                        <div class="echartBox" id="container" style="width: 100%;height:100%;"></div>' +
            '                        <div class="tableBox" id="table" style="width: 100%;height:100%;">' +
            '<div class="scroll">'+
            '                            <div id="title"></div>'+
            '                            <table>' +
            '                                <thead>' +
            '                                <tr>' +
            '                                    <td>科室</td>' +
            '                                    <td>值</td>' +
            '                                    <td>差值</td>' +
            '                                    <td>描述</td>' +
            '                                </tr>' +
            '                                </thead>' +
            '                                <tbody>' +
            '                                </tbody>' +
            '                            </table>' +
            '                      </div>  </div>' +
            '                    </div>');
        cont.append(box);
        if(index==dataList.length-1){
            box.addClass("chartBoxLast");
        }
        var option = {
            title: {
                text: datas[0].typeName,
                left: 'center'
            },
            legend: {
                data:['科室']
            },

            yAxis: {
                data: chartDataItem,
                axisLabel:{
                    formatter:function (value) {
                        var maxlength = 6;
                        if (value.length > maxlength) {
                            return value.substring(0, maxlength - 1) + '...';
                        } else {
                            return value;
                        }
                        ;
                    }
                }
            },
            xAxis: {
                max:maxValue
            },
            dataZoom: [
                {
                    type: 'slider', //外置 界面中显示一个滚动条用来操作缩放
                    show: true,
                    yAxisIndex: [0],
                    left: '93%',
                    start: 100, //数据窗口范围的起始百分比
                    end: 70
                },
                {
                    type: 'inside',//内置 用鼠标拖拽图表即可完成滚动
                    yAxisIndex: [0],
                    start: 0,
                    end: 50
                }
            ],
            grid: {
                left: 110,
                right:60
            },
            series: [{
                //name: '科室',
                type: 'bar',
                barWidth:15,
                data: chartDataValue,
                itemStyle: {
                    normal: {
                        color: "#4268a0",
                        label: {
                            show: true, //开启显示
                            position: 'right', //在上方显示
                            formatter:'{b}：{c}',
                            textStyle: { //数值样式
                                color: '#4268a0',
                                fontSize: 12
                            }
                        }
                    }
                },
                markPoint : {
                    data : [
                        {type : 'max', name: '最大值'},
                        {type : 'min', name: '最小值'}
                    ]

                },
                markLine: {
                    data: [{
                        silent: true,
                        xAxis: datas[0].normal
                    }]
                }
            }]
        }

        var dom = box.find("#container")[0];
        var chart = echarts.init(dom);
        chart.setOption(option, true);
        chartList.push(chart);

        var table = box.find("tbody");
        for (var i = 0; i < datas.length; i++) {
            var trs = $("<tr>" +
                "<td>" + datas[i].deptName + "</td>" +
                "<td>" + datas[i].nowP + "</td>" +
                "<td>" + datas[i].difference + "</td>" +
                "<td>" + datas[i].now + "</td>" +
                "</tr>");
            table.append(trs);
        }
        box.find("#title").html(datas[0].typeName);

    })
}


